package com.ruoyi.web.controller.monitor;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.framework.web.service.CacheService;
import net.oschina.j2cache.CacheChannel.Region;
import net.oschina.j2cache.CacheObject;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * @description: 缓存监控
 * @author: wenchao
 * @date: 2019-11-16
 */
@Controller
@RequestMapping("${sys-props.adminPath}/monitor/cache")
public class CacheController extends BaseController {
    private String prefix = "monitor/cache";

    @Autowired
    public CacheService cacheService;

    @GetMapping()
    public String cache() {
        return prefix + "/cache";
    }

    @RequiresPermissions("monitor:cache:list")
    @RequestMapping({"cacheNameList"})
    @ResponseBody
    public TableDataInfo cacheNameList() {
        Collection<Region> list = this.cacheService.getCacheNameList();
        return getDataTable((List) list);
    }

    @RequiresPermissions("monitor:cache:list")
    @RequestMapping({"cacheKeyList"})
    @ResponseBody
    public TableDataInfo cacheKeyList(String cacheName) {
        List<Map<String, String>> list = this.cacheService.getCacheKeyList(cacheName);
        return getDataTable(list);
    }


    @RequiresPermissions("monitor:cache:list")
    @RequestMapping({"cacheValue"})
    @ResponseBody
    public CacheObject cacheValue(String cacheName, String key) {
        return this.cacheService.getCacheValue(cacheName, key);
    }

    @RequiresPermissions("monitor:cache:remove")
    @RequestMapping({"clearCache"})
    @ResponseBody
    public AjaxResult clearCache(String cacheName, String key) {
        this.cacheService.clearCache(cacheName, key);
        return AjaxResult.success("清除缓存成功");
    }
}
